package com.mall.order.biz.handler;

import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CartProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * ciggar
 * create-date: 2019/8/1-下午5:01
 * 初始化订单 生成订单
 * 数据库中的插入操作
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;


    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        // 插入订单表
        Order order = new Order();
        //全局ID生成器（后续改为使用发号器）  生成唯一ID的工具
        String orderId = UUID.randomUUID().toString();

        order.setOrderId(orderId);
        order.setUserId(createOrderContext.getUserId());
        order.setBuyerNick(createOrderContext.getBuyerNickName());
        order.setPayment(createOrderContext.getOrderTotal());   //订单总金额
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);

        //插入订单
        orderMapper.insert(order);

        List<Long> buyProductIds = new ArrayList<>();

        // 插入订单商品关联表
        List<CartProductDto> productDtoList = createOrderContext.getCartProductDtoList();
        for (CartProductDto cartProductDto : productDtoList) {

            OrderItem orderItem = new OrderItem();
            String orderItemId = UUID.randomUUID().toString();

            orderItem.setId(orderItemId);   //id主键
            orderItem.setItemId(cartProductDto.getProductId());     //商品Id
            orderItem.setOrderId(orderId);      //订单Id
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setPicPath(cartProductDto.getProductImg());

            BigDecimal total = cartProductDto.getSalePrice().multiply(new BigDecimal(cartProductDto.getProductNum()));
            orderItem.setTotalFee(total);     //商品总价格
            orderItem.setStatus(1);     //已锁定库存
            buyProductIds.add(cartProductDto.getProductId());

            //插入
            orderItemMapper.insert(orderItem);

            //设置没有的数据
            createOrderContext.setOrderId(orderId);//

            createOrderContext.setBuyProductIds(buyProductIds);

        }


        return true;
    }
}
